/**
 * @author sjp
 * @date 2021/10/12 21:58
 * @description 3.编写一个死锁程序
 */
public class ThreadTest {

    public static void main(String[] args) {

        //定义两个锁对象
        Object lockA = new Object();
        Object lockB = new Object();


        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (lockA){
                        System.out.println(Thread.currentThread().getName()+"获取锁A");

                        //让当前线程睡眠100ms
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName()+"等待获取锁B");
                        synchronized (lockB){
                            System.out.println(Thread.currentThread().getName()+"获取锁B");
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (lockB){
                        System.out.println(Thread.currentThread().getName()+"获取锁B");

                        //让当前线程睡眠100ms
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName()+"等待获取锁A");

                        synchronized (lockA){
                            System.out.println(Thread.currentThread().getName()+"获取锁A");
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread2.start();


    }
}
